<?php

class genser {
	public $eols;
	public $socket;

	function __construct($server, $port) {
		// connect to server
		$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		socket_connect($this->socket, $server, $port);

		// define default chars which indicate a full packet is recv'd
		$this->eols = array();
		$this->addEOL("\r\n");
		$this->addEOL("\n");
	}

	function __destruct() {
		if($this->socket != null) {
			socket_shutdown($this->socket, 1);
			socket_close($this->socket);
		}
	}

	// adjust what strings indicate we've got a full command, process it
	function addEOL($eol) {
		$this->eols[] = $eol;
	}

	// adjust what strings indicate we've got a full command, process it
	function delEOL($eol) {
		$_eols = array();

		// build a new array, avoid adding the eol being removed
		foreach($this->eols as $next) {
			if(strcmp($next, $eol)) {
				$_eols[] = $next;
			}
		}

		$this->eols = $_eols;
	}

	// checks if there is network data ready and if so
	// returns the result as parsed xml, ready to use
	//
	// ** blocks if data exists but a full packet is not yet ready
	function get($data) {
		// if the socket is alive
		if($this->socket) {
			// check if there is data ready to be read
			$r = array( $this->socket );
			$w = NULL;
			$e = NULL;
			$count = socket_select($r, $w, $e, 0);

			// check if the socket is ready to be read
			if($count) {

			// read one character at a time watching until a full packet is recv'd
			$buffer = "";
			$chr = socket_read($this->socket, 1, PHP_BINARY_READ);
			$ready = false;
			while($chr !== false) {
				// append the new char
				$buffer .= $chr;

				// test for end of packet
				foreach($this->eols as $eol) {
					if(strpos($buffer, $eol) !== false) {
						$ready = true;
						break;
					}
				}

				if($ready) { break; }
				$chr = socket_read($this->socket, 1, PHP_BINARY_READ);
			}

			// at this point we have a full packet ready to go
			return $buffer;

			}
		}
	}

	// performs an ask returning the result as parsed xml
/*	function get_xml($data) {
		return simplexml_load_string( get($data) );
	}
*/

	// send request to server and returns the result as parsed xml, ready to use
	//
	// ** blocks if there is no response from the server
	function ask($data) {
		// if the socket is alive
		if($this->socket) {
			// send the data to the server
			socket_write($this->socket, $data, strlen($data));

			// read one character at a time watching until a full packet is recv'd
			$buffer = "";
			$chr = socket_read($this->socket, 1, PHP_BINARY_READ);
			$ready = false;
			while($chr !== false) {
				// append the new char
				$buffer .= $chr;

				// test for end of packet
				foreach($this->eols as $eol) {
					if(strpos($buffer, $eol) !== false) {
						$ready = true;
						break;
					}
				}

				if($ready) { break; }
				$chr = socket_read($this->socket, 1, PHP_BINARY_READ);
			}

			// at this point we have a full packet ready to go
			return $buffer;
		}
	}

	// performs an ask returning the result as parsed xml
/*	function ask_xml($data) {
		$buffer = ask($data);
		$result = simplexml_load_string( $buffer );

		//return simplexml_load_string( $buffer );
		return $result;
	}
*/
}

?>
